package com.leorui.schoollife.controller.impl

import com.leorui.schoollife.controller.IPersonalInfoControl
import com.leorui.schoollife.entity.PersonalInfoResponse
import com.leorui.schoollife.entity.response.CommonResponse
import com.leorui.schoollife.mapper.IDBIcons
import com.leorui.schoollife.mapper.IDBPersonalInfo
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody

@Controller
class PersonalInfoControllerImpl : IPersonalInfoControl {
    @Autowired(required = false)
    var dbPersonalInfo: IDBPersonalInfo? = null

    @Autowired(required = false)
    var dbIcons: IDBIcons? = null

    // 获取个人信息
    @ResponseBody
    @PostMapping("/api/v1/pull_personal_info")
    override fun getPersonalInfo(@RequestParam(name = "account", required = true) account: String): Map<Any?, Any?> {
        val response: PersonalInfoResponse?
        try {
            response = dbPersonalInfo?.getPersonalInfo(account)
                    ?: return CommonResponse(1, "error1: account: $account", false).toMap()
            response.friendCircleCount = dbPersonalInfo?.getPersonalFriendCircleCount(account)
                    ?: return CommonResponse(1, "error2: account: $account", false).toMap()
            response.status = 0
        } catch (e: Exception) {
            // 异常错误
            return CommonResponse(2, e.message, false).toMap()
        }
        return response.toMap()
    }

    // 更改个人信息
    @ResponseBody
    @PostMapping("/api/v1/modify_personal_info")
    override fun modifyPersonalInfo(
            @RequestParam(value = "account", required = true) account: String,
            @RequestParam(value = "personal_profile", required = true) personalProfile: String,
            @RequestParam(value = "school_id", required = true) schoolId: Int,
            @RequestParam(value = "icon_id", required = true) icon: String,
            @RequestParam(value = "sex", required = true) sex: Int,
            @RequestParam(value = "area", required = true) area: String,
            @RequestParam(value = "phone", required = true) phone: String,
            @RequestParam(value = "nickname", required = true) nickname: String,
            @RequestParam(value = "student_id", required = true) studentId: String,
            @RequestParam(value = "teacher_id", required = false) teacherId: String?,
            @RequestParam(value = "teach_class", required = false) teachClass: String?,
    ): Map<Any?, Any?> {
        dbPersonalInfo?.updatePersonalInfoStudent(personalProfile, schoolId, sex, area, icon, nickname, studentId, phone, account)
                ?: return CommonResponse(1, "服务器问题", false).toMap()
        if (teacherId != null && teachClass != null) {
            dbPersonalInfo?.updatePersonalInfoTeacher(teachClass, account, teacherId)
                    ?: return CommonResponse(1, "服务器问题", false).toMap()
        }
        return CommonResponse(0, null, true).toMap()
    }

    @ResponseBody
    @PostMapping("/api/v1/change_icon")
    override fun modifyPersonalIcon(
            @RequestParam(value = "account", required = true) account: String,
            @RequestParam(value = "icon_id", required = true) icon: String
    ): Map<Any?, Any?> {
        return try {
            dbIcons?.updateUserIcon(account, icon)
            CommonResponse(0, null, false).toMap()
        } catch (e: Exception) {
            CommonResponse(1, "${e.message}", false).toMap()
        }
    }
}